<?php

if(!defined("INCLUDE")) die("Nedostatečné oprávnění pro spouštění souboru.");

# --------------------------------------------------------------------------------------------------------------------------
# CLASS PRESENTATION
# --------------------------------------------------------------------------------------------------------------------------
# Třída slouží k vykreslování požadovaných formulářů a hlášení, které se následně volají v procesoru.
#
# --------------------------------------------------------------------------------------------------------------------------
# INFORMACE O SOUBORU
# --------------------------------------------------------------------------------------------------------------------------
# Autor: Zechy
# Poslední úprava: 1. 12. 2012
#

class presentation
{
	public function index($entity, $newest_comments, $mostreaded, $exception = null, $exception_messsage = null)
	{
		isset($_GET["page"]) ? $page = $_GET["page"] : $page = 1;
		include ROOT . DS . "application" . DS . "articles" . DS . "views" . DS . "index.php";
	}

	public function add_article()
	{
		$this->article_form();
	}

	public function edit_article($entity)
	{
		$this->article_form($entity->get_heading(), $entity->get_category(), $entity->get_content("both"), $entity->get_tags(), $entity->get_datetime("date"), $entity->get_datetime("time"));
	}

	public function delete_article($sql_result, $exception = null, $exception_messsage = null)
	{
		if(!is_null($exception))
		{
			if($exception == "empty_result")
			{
				$this->reporting("warning", "article_notfound");
			}
			else if($exception == "thrown_exception")
			{
				echo '<p class="ui-state-error ui-corner-all">'.$exception_messsage."</p>";
			}
		}
		else
		{
			$row = mysql_fetch_array($sql_result);
			echo "
			<form action='index.php?modul=articles&action=delete_article' method='post'>
				<input type='hidden' name='id' value='$_GET[id]'>
				<h1 align='center'><img src='theme/ikonky/page_white_delete.png'> Smazání článku</h1>
				<p align='center'>Opravdu chcete smazat článek '<b>$row[heading]</b>'?</p>
				<p align='center'><b>Pozn.:</b> Článek je možné kdykoli obnovit z admin logu.</p>
				<p align='center'><button type='submit' class='button'>
					<img src='theme/ikonky/page_white_delete.png'> Smazat článek
				</button></p>
			</form>
			";
		}
	}

	public function category_view($category_list, $exception = null, $exception_messsage = null)
	{
		include ROOT . DS . "application" . DS . "articles" . DS . "views" . DS . "category_index.php";
	}

	public function add_category()
	{
		$this->category_form();
	}

	public function edit_category($sql_result, $exception = null, $exception_messsage = null)
	{
		if(!is_null($exception))
		{
			if($exception == "empty_result")
			{
				echo '<p class="ui-state-highlight ui-corner-all"><img src="theme/ikonky/folder_error.png"> Kategorie s požadovaným ID nebyla v databázi nalezena.</p>';
			}
			else if($exception == "thrown_exception")
			{
				echo '<p class="ui-state-error ui-corner-all">'.$exception_messsage."</p>";
			}
		}
		else
		{
			$row = mysql_fetch_array($sql_result);
			
			if($row["id"] == 1) echo '<p class="ui-state-error ui-corner-all"><img src="theme/ikonky/exclamation.png" align="absmiddle"><b>Nepovolená akce</b>: Defaultní kategorii nelze upravit.</p>';
			else $this->category_form($row["name"], $row["note"]);
		}
	}

	public function delete_category($sql_result, $exception = null, $exception_messsage = null)
	{
		if(!is_null($exception))
		{
			if($exception == "empty_result")
			{
				echo '<p class="ui-state-highlight ui-corner-all"><img src="theme/ikonky/folder_error.png"> Kategorie s požadovaným ID nebyla v databázi nalezena.</p>';
			}
			else if($exception == "thrown_exception")
			{
				echo '<p class="ui-state-error ui-corner-all">'.$exception_messsage."</p>";
			}
		}
		else
		{
			$row = mysql_fetch_array($sql_result);
			echo "
			<form action='index.php?modul=articles&action=delete_category' method='post'>
				<input type='hidden' name='id' value='$_GET[id]'>
				<h1 align='center'><img src='theme/ikonky/folder_delete.png'> Smazání kategorie</h1>
				<p align='center'>Opravdu chcete smazat kategorii '<b>$row[name]</b>'?</p>
				<p align='center'><b>Pozn.:</b> Všechny články z kategorie budou přesunuty do kategorie Nezařazené.</p>
				<p align='center'><button type='submit' class='button'>
					<img src='theme/ikonky/folder_delete.png'> Smazat kategorii
				</button></p>
			</form>
			";
		}
	}

	private function article_form($heading = "", $category = "", $content = "", $tags = "", $date = "", $time = "")
	{
		include ROOT . DS . "application" . DS . "articles" . DS . "views" . DS . "article_form.php";
	}
	
	private function category_form($name = "", $note = "")
	{
		include ROOT . DS . "application" . DS . "articles" . DS . "views" . DS . "category_form.php";
	}

	public function page_list($page)
	{
		$mapper = new mapper;
		$settings = new settings;

		$count = $mapper->articles_count();
		$onPage = $settings->get_setting("nastaveni_clanku", "kolik_na_stranku");

		if($count != false)
		{
			if($count > $onPage)
			{
				$return = "<div class='buttonset'><p align='center'>";
				for($a = 1; $a <= ceil($count/$onPage); $a++)
				{
					if($page == $a)
					{
						$return .= "<a href='index.php?modul=articles&page=$a' class='ui-state-hover'>$a</a>";
					}
					else
					{
						$return .= "<a href='index.php?modul=articles&page=$a'>$a</a>";
					}
				}
				$return .= "</p></div>";
				return $return;
			}
		}
		else
		{
			return "Nepodařilo se vytvořit list stránek.";
		}
	}

	public function reporting($action, $for)
	{
		switch($action)
		{
			case "empty":
				echo "<p class='ui-state-highlight ui-corner-all'><img src='theme/ikonky/error.png' align='absmiddle'> ".$_SESSION[$action][$for]."</p>";
				break;

			case "success":
				echo "<p class='uspech ui-corner-all'><img src='theme/ikonky/tick.png' align='absmiddle'> ".$_SESSION[$action][$for]."</p>";
				break;

			case "warning":
				echo "<p class='ui-state-highlight ui-corner-all'><img src='theme/ikonky/error.png' align='absmiddle'> ".$_SESSION[$action][$for]."</p>";
				break;

			case "error":
				break;
		}
	}
}

?>